import moment from 'moment'

function pad (num) {
  return num < 10 ? '0' + num : num
}

export function timeFormat (text) {
  return 'YYYY.MM.DD H:mm'
}

export function formatDuring (mms, d = 1) {
  if (mms < 0) {
    mms = 0
  }

  const hours = parseInt(mms * d / (1000 * 60 * 60))
  const minutes = parseInt((mms * d % (1000 * 60 * 60)) / (1000 * 60))
  const seconds = parseInt((mms * d % (1000 * 60)) / 1000)

  return `${ pad(hours) }:${ pad(minutes) }:${ pad(seconds) }`
}

export function calcTime (preSelectDate, preSelectEndDate, hobSpecificTime, hobSpecificEndTime) {
  // 是否支持预选
  let canPreSelect = false
  // 是否支持选房
  let canHobSpecific = false
  // 是否开始预选
  let isPreSelect = false
  // 预选是否过期
  let isPreSelectTimeout = false
  // 是否开始选房
  let isHobSpecific = false
  // 选房是否过期
  let isHobSpecificTimeout = false

  // 备选开始差多少毫秒
  let startDiffPreSelect = -1
  // 备选结束差多少毫秒
  let endDiffPreSelect = -1
  // 已经备选结束多少毫秒
  let diffPreSelectTimeout = -1

  // 选房开始差多少毫秒
  let startDiffHobSpecific = -1
  // 选房结束差多少毫秒
  let endDiffHobSpecific = -1
  // 选房已超时多少毫秒
  let diffHobSpecificTimeout = -1

  const now = moment()
  const mPreSelectDate = moment(preSelectDate)
  const mPreSelectEndDate = moment(preSelectEndDate)
  const mHobSpecificTime = moment(hobSpecificTime)
  const mHobSpecificEndTime = moment(hobSpecificEndTime)

  if (mPreSelectDate.isValid()) {
    canPreSelect = true

    // 当前时间在备选开始时间前
    if (now.isBefore(mPreSelectDate)) {
      startDiffPreSelect = mPreSelectDate.diff(now)
    } else {
      // 没有备选结束时间
      if (!mPreSelectEndDate.isValid()) {
        // 没有设置结束时间但是时间已经开始则说明现在可以备选
        if (now.isAfter(mPreSelectDate)) {
          isPreSelect = true
        }
      } else {
        if (now.isBetween(mPreSelectDate, mPreSelectEndDate)) {
          isPreSelect = true
          endDiffPreSelect = mPreSelectEndDate.diff(now)
        } else {
          isPreSelectTimeout = true
          diffPreSelectTimeout = now.diff(mPreSelectEndDate)
        }
      }
    }
  }

  // 只要设置了选房时间就支持选房
  if (mHobSpecificTime.isValid()) {
    canHobSpecific = true

    // 还没到选房开始时间
    if (now.isBefore(mHobSpecificTime)) {
      startDiffHobSpecific = mHobSpecificTime.diff(now)
    } else {
      isPreSelect = false

      // 没有选房结束时间
      if (!mHobSpecificEndTime.isValid()) {
        // 没有设置选房结束时间但是选房时间已经开始则说明现在可以选房
        if (now.isAfter(mHobSpecificTime)) {
          isHobSpecific = true
        }
      } else {
        // 有选房结束时间
        if (now.isBetween(mHobSpecificTime, mHobSpecificEndTime)) {
          isHobSpecific = true
          endDiffHobSpecific = mHobSpecificEndTime.diff(now)
        } else {
          isHobSpecificTimeout = true
          diffHobSpecificTimeout = now.diff(mHobSpecificEndTime)
        }
      }

    }

  }

  return {
    canPreSelect,
    canHobSpecific,
    isPreSelect,
    isPreSelectTimeout,
    isHobSpecific,
    isHobSpecificTimeout,

    startDiffPreSelect,
    endDiffPreSelect,
    startDiffHobSpecific,
    endDiffHobSpecific,

    mPreSelectDate,
    mPreSelectEndDate,
    mHobSpecificTime,
    mHobSpecificEndTime,

    diffPreSelectTimeout,
    diffHobSpecificTimeout,
  }
}
